package skins
{
	import mx.states.State;
	
	import spark.components.Button;
	import spark.components.supportClasses.Skin;
	import spark.layouts.HorizontalLayout;
	
	// Sometimes mxmlc breaks here for no apparent reason
	// it's uncommon to write skins in AS - so the compiler
	// wasn't proeprly tested to do it. So it breaks
	// on the very basic things...
	// Technically, you should be able to uncomment this,
	// but for me it mostly won't compile, if I do.
	//[HostComponent("actionscript.StatefulExample")]
	
	[States("whatever", "somethingElse")]
	
	public class ASSkin extends Skin
	{
		private var _clickMe:Button = new Button();
		private var _includeMe:Button = new Button();
		private var _excludeMe:Button = new Button();
		
		public function ASSkin()
		{
			super();
			this.init();
		}
		
		public override function set currentState(value:String):void
		{
			if (value == "whatever" || value == "somethingElse")
			{
				if (value == "whatever")
				{
					if (super.owns(this._excludeMe))
						super.removeElement(this._excludeMe);
					super.addElement(this._includeMe);
					this._clickMe.label = "Whatever";
				}
				else
				{
					if (super.owns(this._includeMe))
						super.removeElement(this._includeMe);
					super.addElement(this._excludeMe);
					this._clickMe.label = "Something else";
				}
			}
			super.currentState = value;
		}
		
		private function init():void
		{
			super.layout = new HorizontalLayout();
			this._clickMe.label = "Whatever";
			this._excludeMe.label = "Excluced";
			this._includeMe.label = "Included";
			super.states = [
				new State({ name: "whatever" }),
				new State({ name: "somethingElse" })];
			this.currentState = "whatever";
		}
		
		public override function validateDisplayList():void
		{
			// Here you can see how validateDisplayList is called
			// too late (after we set the currentState.
			super.addElement(this._clickMe);
			super.validateDisplayList();
		}
	}
}